package red.internal.core.util;

import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * @author Jin Zheng
 * @since 1.0 2020-05-29
 */
public class RandomUtil
{
	private static final Random RANDOM = new Random();

	private RandomUtil()
	{
	}


	public static <T> T getOne(Set<T> set)
	{
		if (set == null || set.isEmpty())
			return null;

		int size = set.size();
		int index = RANDOM.nextInt(size);
		Iterator<T> iterator = set.iterator();
		T target = null;
		while (index >= 0 && iterator.hasNext())
		{
			target = iterator.next();
			index--;
		}
		return target;
	}

	public static <T> T getOne(List<T> list)
	{
		if (list == null || list.isEmpty())
			return null;

		int size = list.size();
		int index = RANDOM.nextInt(size);
		return list.get(index);
	}

}
